﻿using IHISCETeim.BLL;
using IHISCETeim.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using IHISCETeim.Library;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace IHISCETeim.Web.AgentSys.VipMgr
{
    public partial class VIPAgentExchangeAdd : AgentPageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.新增 };
            }
        }
        protected override string DWZRef
        {
            get { return "VIPAgentExchangeList"; }

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "add")
            {
                add();//
            }
        }

        private void add()
        {


            if (!HasPower(IHISCETeim.Web.Permission.新增))
            {
                ErrorJson("你没有权限进行此操作");
                return;
            }

            VIPAgentExchangeInfo obj = Request.SerializableObject<VIPAgentExchangeInfo>();
            obj.UserId = base.User.UserId;
            obj.AgentName = base.User.Department;
            //obj.AgentTotalPoint = obj.TotalPoint;
            //积分判断处理
            VIPAgentExInfo exAgentObj = VIPAgentExController.GetListByUserID(obj.UserId);
            
            if (exAgentObj.AvailablePoint < obj.TotalPoint)
            {
                AlertJson("对不起,您的积分不足于兑换此批配件.");
            }
            if (exAgentObj.CustomerExChangePoint < obj.TotalPoint)
            {
                AlertJson("对不起,您的VIP客户积分兑换值不足于用在本次此批配件兑换,<br/>注：需要VIP客户先兑换，您才能申请相应的积分兑换配件操作.");
            }

            char[] caSplit = new char[] { ',' };
            string[] ItemCodes = GetRequestValue<string>("ItemCode", "").Split(caSplit);
            string[] ItemNames = GetRequestValue<string>("ItemName", "").Split(caSplit);
            string[] Prices = GetRequestValue<string>("Price", "").Split(caSplit);
            string[] Quantitys = GetRequestValue<string>("Quantity", "").Split(caSplit);
           
            string[] LineTotals = GetRequestValue<string>("LineTotal", "").Split(caSplit);
            string[] Points = GetRequestValue<string>("Point", "").Split(caSplit);

            List<VIPAgentExchangeDetailInfo> list = new List<VIPAgentExchangeDetailInfo>();
            for (int rowIndex = 0; rowIndex < ItemCodes.Count(); rowIndex++)
            {
                VIPAgentExchangeDetailInfo itm = new VIPAgentExchangeDetailInfo();
                itm.ItemCode = ItemCodes[rowIndex];
                itm.ItemName = ItemNames[rowIndex];
                itm.Price = Convert.ToDecimal(Prices[rowIndex]);
                itm.Quantity = Convert.ToDecimal(Quantitys[rowIndex]);
             
                itm.LineTotal = Convert.ToDecimal(LineTotals[rowIndex]);
                itm.LinePoint = Convert.ToInt32(Points[rowIndex]);
             
                list.Add(itm);
            }


            int R = VIPAgentExchangeController.Add(obj, list);
            switch (R)
            {
                case 1:
                    SuccessJson("已成功申请积分兑换配件信息,同时邮件通知IHISCE配件部审核.");
                    break;
                case 0:
                    ErrorJson("添加申请积分兑换配件信息失败");
                    break;
            }
        }
    }
}